package com.jcfk.eam.controller.parent;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.base.EamAssetTypeDTO;
import com.jcfk.eam.domain.query.base.EamAssetTypeQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.service.base.EamAssetTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 资产类型 服务接口
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-04-24
 */
@Api(tags = {"资产类型"})
@RestController
@RequestMapping("eamAssetType")
public class EamAssetTypeController {
    @Autowired
    private EamAssetTypeService eamAssetTypeService;

    /**
     * 查询量检具类型列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EamAssetTypeQuery", name = "query", value = "", required = true)})
    @ApiOperation(value = "查询量检具类型列表", notes = "查询量检具类型列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamAssetTypeDTO>> getList(@RequestBody EamAssetTypeQuery query) {
        List<EamAssetTypeDTO> list = eamAssetTypeService.getList(query);
        return RetResult.ok(list);
    }

    /**
     * 获取量检具类型树
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EamAssetTypeQuery", name = "ids", value = "", required = true)})
    @ApiOperation(value = "获取量检具类型树", notes = "获取量检具类型树", httpMethod = "POST")
    @PostMapping("getTree")
    public RetResult<List<EamAssetTypeDTO>> getTree(@RequestBody EamAssetTypeQuery query) {
        return RetResult.ok(eamAssetTypeService.getTree(query));
    }

    /**
     * 获取可用量检具类型树
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EamAssetTypeQuery", name = "ids", value = "", required = true)})
    @ApiOperation(value = "获取可用量检具类型树", notes = "获取量检具类型树", httpMethod = "POST")
    @PostMapping("getAvailableTree")
    public RetResult<List<EamAssetTypeDTO>> getAvailableTree(@RequestBody EamAssetTypeQuery query) {
        query.setEnabled(AvailableStatus.ENABLE.getValue());
        return RetResult.ok(eamAssetTypeService.getTree(query));
    }

    /**
     * 获取可用量检具类型树
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EamAssetTypeQuery", name = "ids", value = "", required = true)})
    @ApiOperation(value = "获取可用量检具类型树", notes = "获取量检具类型树", httpMethod = "POST")
    @PostMapping("getAvailableList")
    public RetResult<List<EamAssetTypeDTO>> getAvailableList(@RequestBody EamAssetTypeQuery query) {
        query.setEnabled(AvailableStatus.ENABLE.getValue());
        return RetResult.ok(eamAssetTypeService.getAvailableList(query));
    }


    /**
     * 获取量检具类型
     * 根据id 查询详情
     */
    @ApiOperation("获取量检具类型")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetTypeDTO> get(@PathVariable("id") String id) {
        EamAssetTypeDTO item = eamAssetTypeService.get(id);
        return RetResult.ok(item);
    }


    @ApiOperation("保存量检具类型")
    @PostMapping("/save")
    @RequestLogHeader(modular = "量检具类型", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetTypeDTO eamAssetType) {
        // 数据校验
        RetResult ret = eamAssetTypeService.isValid(eamAssetType);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }
        // 数据校验
        if (StrUtil.isEmpty(eamAssetType.getId())) {
            // 新增
            eamAssetTypeService.insert(eamAssetType);
        } else {
            // 更新
            eamAssetTypeService.update(eamAssetType);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除量检具类型
     */
    @ApiOperation("批量删除量检具类型")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "量检具类型", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetTypeDTO dto) {
        // 校验
        for (String id : dto.getIds()) {
            Boolean haveChild = eamAssetTypeService.isHaveChild(id);
            if (haveChild.equals(true)) {
                return RetResult.fail("不能删除有下级的量检具类型");
            }
        }
        return RetResult.ok(eamAssetTypeService.logicDeleteList(dto));
    }

}
